package interfaces;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import model.AlarmClockWrapper;
import model.AlarmClockWrapperColumn;
import model.EditType;
import model.EnumMapper;
import model.ListTableModel;
import services.UserDataServicesProxy;
import controller.AlarmClockController;
/**
 * @author км
 *
 */
public class AlarmClockEditor extends JPanel {
	private static final long serialVersionUID = 1L;
	private JTable table;
	private ListTableModel<AlarmClockWrapper> model;
	private AlarmClockController controller;
	public AlarmClockEditor(AlarmClockController controller) {
		super(new GridBagLayout());
		this.controller = controller;
		initComponents();
	}

	private void initComponents() {
		JPanel panel = new JPanel(new BorderLayout());
		EnumMapper<AlarmClockWrapper> of = EnumMapper.of(AlarmClockWrapperColumn.class);
		model = ListTableModel.from(controller.getData(), of);
		table = model.makeTable();
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		panel.add(new JScrollPane(table), BorderLayout.CENTER);
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		JButton addButton = new JButton("Добавить");
		addButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				AlarmClockWrapper showDialog = AlarmClockEditorDialog.showDialog(
						MainFrame.getUser(), new AlarmClockWrapper(), EditType.ADD, controller.getTypes());
				if(showDialog != null) {
					controller.getData().add(showDialog);
					model.add(showDialog);
				}
			}
		});
		JButton editButton = new JButton("Редактировать");
		editButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int selectedRow = table.getSelectedRow();
				if(selectedRow == -1) {
					JOptionPane.showMessageDialog(null, "Вам нужно выбрать строку в таблице");
					return;
				} else {
					AlarmClockWrapper userDataWrapper = model.get(selectedRow);
					AlarmClockEditorDialog.showDialog(MainFrame.getUser(), userDataWrapper, EditType.EDIT, controller.getTypes());
					model.fireTableDataChanged();
				}
			}
		});
		JButton deleteButton = new JButton("Удалить");
		deleteButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int selectedRow = table.getSelectedRow();
				if(selectedRow == -1) {
					JOptionPane.showMessageDialog(null, "Вам нужно выбрать строку в таблице");
					return;
				}
				int result = MyConfirmDialog.showDialog("Вы действительно хотите удалить данную строку?", "Предупредление",
						JOptionPane.INFORMATION_MESSAGE, null);
				if(result != JOptionPane.YES_OPTION)
					return;
				AlarmClockWrapper userDataWrapper = model.get(selectedRow);
				try {
					new UserDataServicesProxy().deleteData(userDataWrapper.getId());
				} catch (RemoteException e1) {
					e1.printStackTrace();
				}
				model.remove(userDataWrapper);
				}
		});
		buttonPanel.add(addButton);
		buttonPanel.add(editButton);
		buttonPanel.add(deleteButton);
		panel.add(buttonPanel, BorderLayout.SOUTH);
		add(panel);
	}
}
